#include "dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent)
{
    label_username = new QLabel("用户名");
    lineEdit_username = new QLineEdit;
   // lineEdit_username->setPlaceholderText("Username");

    label_passwd = new QLabel(" 密码 ");
    lineEdit_passwd = new QLineEdit;
   // lineEdit_passwd->setPlaceholderText("Password");
    lineEdit_passwd->setEchoMode(QLineEdit::Password);

    login_Button = new QPushButton("登录");
    register_Button = new QPushButton("注册");

    QHBoxLayout *nameLayout = new QHBoxLayout;
    nameLayout->addWidget(label_username);
    nameLayout->addWidget(lineEdit_username);

    QHBoxLayout *passwdLayout = new QHBoxLayout;
    passwdLayout->addWidget(label_passwd);
    passwdLayout->addWidget(lineEdit_passwd);

    QHBoxLayout *logregLayout = new QHBoxLayout;
    logregLayout->addWidget(login_Button);
    logregLayout->addWidget(register_Button);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(nameLayout);
    mainLayout->addLayout(passwdLayout);
    mainLayout->addLayout(logregLayout);

    setLayout(mainLayout);
    setWindowTitle("Chat");

    socket = new QTcpSocket;

    setWindowIcon(QIcon(":/QQ.png"));

    connect(login_Button, SIGNAL(clicked(bool)), this, SLOT(login_Button_clicked()));
    connect(register_Button, SIGNAL(clicked(bool)), this, SLOT(register_Button_clicked()));
}

Dialog::~Dialog()
{

}

void Dialog::login_Button_clicked()
{
//    QString IP = "60.205.176.175";
//    int port = 8001;

    memset(&usr_data,0,DATASIZE);


    /* 若输入为空 显示错误信息 */
    QString username = lineEdit_username->text();
    if(username.isEmpty())
    {
         QMessageBox::information(NULL, "错误", "请输入用户名!");
         return ;
    }

    QString passwd = lineEdit_passwd->text();
    if(passwd.isEmpty())
    {
         QMessageBox::information(NULL, "错误", "请输入用密码!");
         return;
    }

    /* 将Qstring的username和passwd转换成char型复制到结构体usr_info里面 */
    QByteArray ba = username.toLatin1();
    char *mm = ba.data();
    memcpy(usr_data.src_id,mm,username.length());

    ba = passwd.toLatin1();
    mm = ba.data();
    memcpy(usr_data.data,mm,passwd.length());

    /* 点击的是Login按钮，cmd='Login' （Login）*/

    unsigned char cmd = CMD_LOGIN;
    memcpy(&usr_data.cmd,&cmd,CMDSIZE);

//    qDebug() << "name:" << usr_data.src_id << endl;
//    qDebug() << "passwd:" << usr_data.data << endl;

    if(-1 == connect_server())
        return ;

    /* 若连接服务器成功，则发送usr_info给服务器，判断用户名和密码是否正确 */
    socket->write((char *)&usr_data, DATASIZE);

    if (false == socket->waitForReadyRead(1000))
    {
        QMessageBox::information(NULL, "超时", "连接服务器超时!");
        return ;
    }

    /* 读取服务器的返回值，为0则验证成功 */
    USER_DATA state;
    memset(&state,0,DATASIZE);
    socket->read((char *)&state, DATASIZE);
//    qDebug() << "OK: " << state.cmd;

    if(CMD_LOGINSUCCESS == state.cmd)
    {
        emit accept(); /* 结束登录窗口 */
    }
    else if(CMD_LOGINFAILED == state.cmd)
    {
        QMessageBox::information(NULL, "错误", "用户名或密码错误!");
        socket->close();
    }
    else if(CMD_REPEATLOGIN == state.cmd)
    {
        QMessageBox::information(NULL, "错误", "用户已登录!");
        socket->close();
    }
}

void Dialog::register_Button_clicked()
{
    memset(&usr_data,0,DATASIZE);

    QString username = lineEdit_username->text();
    if(username.isEmpty())
    {
         QMessageBox::information(NULL, "错误", "请输入用户名!");
         return ;
    }

    QString passwd = lineEdit_passwd->text();
    if(passwd.isEmpty())
    {
         QMessageBox::information(NULL, "错误", "请输入密码!");
         return ;
    }

    QByteArray ba = username.toLatin1();
    char *mm = ba.data();
    memcpy(usr_data.src_id,mm,username.length());

    ba = passwd.toLatin1();
    mm = ba.data();
    memcpy(usr_data.data,mm,passwd.length());

    unsigned char cmd = CMD_REGISTER;
    memcpy(&usr_data.cmd,&cmd,CMDSIZE);

    qDebug() << "name:" << usr_data.src_id << endl;
    qDebug() << "passwd:" << usr_data.data << endl;

    if(-1 == connect_server())
        return ;

    socket->write((char *)&usr_data, DATASIZE);

    if (false == socket->waitForReadyRead(1000))
    {
        QMessageBox::information(NULL, "超时", "连接服务器超时!");
        return ;
    }

    USER_DATA state;

    socket->read((char *)&state, DATASIZE);
//    qDebug() << "state.cmd: " << state.cmd;
    if(CMD_REGISTERSUCCESS == state.cmd)
    {
        QMessageBox::information(NULL, "OK", "注册成功!");
        socket->close();
    }
    else if(CMD_REGISTEREXISTS == state.cmd)
    {
        QMessageBox::information(NULL, "错误", "用户名已存在!"); /* 用户名已存在 */
        socket->close();
    }
    else if(CMD_REGISTERFAILED == state.cmd)
    {
        QMessageBox::information(NULL, "错误", "数据库错误!");
        socket->close();
    }

}

int Dialog::connect_server()
{
    QString IP = "60.205.176.175";
    int port = 8001;
    socket->connectToHost(IP,port);
    if(!socket->waitForConnected(10000))
    {
//        qDebug() << "Connection failed!";
        QMessageBox::information(NULL, "超时", "连接服务器超时!");
        return -1;
    }
    return 0;
}

void Dialog::closeEvent(QCloseEvent *e)
{
//    qDebug() << "close dialog";
    usr_data.cmd = CMD_CLOSECHAT;
    socket->write((char *)&usr_data, DATASIZE);
    socket->close();
}
